package com.ax.base.common.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.ax.base.common.domain.BaseOrg;
import com.ax.base.common.domain.Tree;
import com.ax.base.common.service.DictionarySubjectService;
import com.ax.base.common.utils.AjaxRes;
import com.ax.base.common.utils.Const;
import com.ax.base.system.domain.MenuDO;

@Controller
@RequestMapping("/sys/org")
public class OrgSkipController {

	@Autowired
	DictionarySubjectService dictionarySubjectService;
	@Autowired
    LoadBalancerClient loadBalancerClient;
    @Autowired
    RestTemplate restTemplate;
    
    
	/**
	 * 菜单页面的跳转
	 */
	@GetMapping()
	String subject(Model model) {
		return "org/organization/organization";
	}
	
	/**
	 * 组织机构查询(模块:组织机构)
	 * 
	 * @param Map<String,Object> map
	 *           
	 * @return BaseOrg baseOrg
	 */
	@RequestMapping("/queryBaseOrg")
	@ResponseBody
	public List<BaseOrg> queryBaseOrg(@RequestParam Map<String,Object> params){
		List<BaseOrg> baseOrgs = this.dictionarySubjectService.queryBaseOrgSer(params);
		return baseOrgs;
	}
	
	/**
	 * 组织机构(模块:组织机构)*跳转增加页面
	 * 
	 * @param Long pId
	 *           
	 * @return BaseOrg baseOrg
	 */
	@GetMapping("/add/{pId}")
	String add(Model model, @PathVariable("pId") Long pId) {
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "根目录");
		} else {
			model.addAttribute("pName",dictionarySubjectService.getBaseOrg(pId).getName());
		}
		return "org/organization/add";
	}
	
	/**
	 * 组织机构新增(模块:组织机构)
	 * 
	 * @param BaseOrg baseOrg
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/insertBaseOrg")
	@ResponseBody
	public AjaxRes insertBaseOrg(AjaxRes ajaxRes,BaseOrg baseOrg){
		try {
			this.dictionarySubjectService.insertBaseOrgSer(baseOrg);
			ajaxRes.setSucceed(Const.SAVE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	/**
	 * 组织机构(模块:组织机构)*跳转修改页面
	 * 
	 * @param Long id
	 *           
	 * @return BaseOrg baseOrg
	 */
	@GetMapping("/edit/{id}")
	String edit(Model model, @PathVariable("id") Long id) {
		model.addAttribute("org",dictionarySubjectService.getBaseOrg(id));
		return "org/organization/edit";
	}
	
	/**
	 * 组织机构修改(模块:组织机构)
	 * 
	 * @param BaseOrg baseOrg
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/updateBaseOrg")
	@ResponseBody
	public AjaxRes updateBaseOrg(AjaxRes ajaxRes,BaseOrg baseOrg){
		try {
			this.dictionarySubjectService.updateBaseOrgSer(baseOrg);
			ajaxRes.setSucceed(Const.UPDATE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	/**
	 * Tree
	 * 
	 * @param Long parentOrgId
	 *           
	 * @return Tree<MenuDO>
	 */
	@GetMapping("/tree/{parentOrgId}")
	@ResponseBody
	Tree<MenuDO> tree(@PathVariable("parentOrgId") Long parentOrgId) {
		Tree<MenuDO> tree = dictionarySubjectService.getTree(parentOrgId);
		return tree;
	}
}
